package com.aimao.blog.config;

import com.aimao.blog.constant.ServerConst;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Amaochen
 * @date 2018/8/2
 * 拦截数据监控 只有管理员登录后才能查看进入
 */
@WebFilter(filterName = "DruidFilter", urlPatterns = {"/druid/*"})
public class DruidFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        if (request.getSession().getAttribute(ServerConst.CURRENT_USER) == null) {
            response.sendError(401);
        } else {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
